package kgame5.enginex {
import kgame5.enginex.enum.Layer3DEnum;
import kgame5.k3d.core.camera.CameraBee;
import kgame5.k3d.core.obj3d.node3d.Group3D;
import kgame5.k3d.core.obj3d.node3d.Node3D;
import kgame5.k3d.core.obj3d.node3d.group3d.FastGroup3D;
import kgame5.k3d.core.obj3d.node3d.group3d.GoodGroup3D;
import kgame5.k3d.core.obj3d.node3d.group3d.World3D;
import kgame5.k3d.core.view3d.View3D;
import kgame5.k3d.util3d.Shaker;
import kgame5.kutil.T;

import flash.display.Sprite;
import flash.utils.Dictionary;

/**
 * @author kk
 */
public class WorldRender {
	//3D区域
	private var view3D:View3D;
	private var world:World3D;
	public var camera:CameraBee;
	
	//各个图层
	private var dict_layer:Dictionary;
	
	public function WorldRender(mc_c:Sprite,viewportWidth:int,viewportHeight:int,bDebugCamera:Boolean=true){
		
		//--3D区域
		view3D=new View3D(viewportWidth,viewportHeight,bDebugCamera);
		world=view3D.world;
		camera=view3D.camera;
		
		//添加
		mc_c.addChild(view3D.getDO());
		
		//屏幕震动器
		Shaker.getInstance().setTarger(view3D.getDO());
		
		//创建各个图层
		dict_layer=new Dictionary();
		
		//添加各个图层。这个是可以修改的
		addLayer(Layer3DEnum.LAYER_GROUND,new FastGroup3D());
		
		addLayer(Layer3DEnum.LAYER_SHADOW,new FastGroup3D());
		
		addLayer(Layer3DEnum.LAYER_MINE,new FastGroup3D());
		
		addLayer(Layer3DEnum.LAYER_GROUNDMARK,new FastGroup3D());//地面被炸的弹坑
		
		addLayer(Layer3DEnum.LAYER_MOUSE3D,new FastGroup3D());
	
		addLayer(Layer3DEnum.LAYER_MAIN,new GoodGroup3D());//游戏区域用的,bars和gu
		
		addLayer(Layer3DEnum.LAYER_TIP,new FastGroup3D());
	}
	
	//添加一个图层。设置此图层内部是否需要排序
	//用法是地面层，地面标识层（如影子），主层（玩家和障碍物）,头顶标识
	public function addLayer(layerID:int,layer:Group3D):void{
		
		dict_layer[layerID]=layer;
		world.addChild(layer);
	}
	
	//将md加入指定图层（分不同图层是为了性能考虑），如无图层将创建新图层
	public function addChild(md:Node3D,layerID:int):void{
		//T.p(this,"addChild",md,layerID);
		if(layerID==-1){
			throw new Error("请设置Node3D的图层！！");
		}
		
		findLayer(layerID).addChild(md);
	}
	
	public function removeChild(md:Node3D):void{
		//T.p(this,"removeChild",md);
		findLayer(md.layerID).removeChild(md);
	}
	
	//直接原因是ParticleEntine要使用这个
	public function findLayer(layerID:int):Group3D{
		var layer:Group3D=dict_layer[layerID];
//		if(layer==null){
//			layer=new FastGroup3D();
//			dict_layer[layerID]=layer;
//			world.addChild(layer);
//		}
		return layer;
	}
	
	//启动渲染
	public function start():void{
		T.p(this,"start");
		//world.removeAllChild();
	}
	
	//停止渲染，清除
	public function clear():void{
		T.p(this,"clear");
		
		Shaker.getInstance().clear();//屏幕震动器
		
		//--清除world里的child
		world.removeAllChild();
		
		dict_layer=null;
	}
	
	//渲染到屏幕
	public function render():void{
		view3D.render();
	}
}//end of class
}

//////世界的长和宽，x+和z-,世界如果是个方块，则原点在左上角
//	public function initCamera(mapWidth:int,mapHeight:int,panRadius:int,cameraY:int):void{
//		//TODO 如何设置摄像机的还没做好
//	
//		//焦点固定
//		camera.target.fill(mapWidth/2, 0, -mapHeight/2);
//		// 摄像机固定
//		camera.radius = 900;//C.CAMERA_RADIUAS;
//		camera.pan=180;
//		camera.y = 900;//C.CAMERA_Y;
//	}
